package pconline;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import entities.ArticleEntity;
import entities.SearchListEntity;

public class Pconline implements IPcnline {

	@Override
	public List<SearchListEntity> getNewsList(int newType, int page) throws IOException {
		String url = "";
		switch (newType) {
		case 0:
			url = "http://news.pconline.com.cn/index_" + page + ".html";
			break;
		case 1:
			url = "http://news.pconline.com.cn/it/index_" + page + ".html";
			break;
		case 2:
			url = "http://news.pconline.com.cn/mobile/index_" + page + ".html";
			break;
		case 3:
			url = "http://news.pconline.com.cn/office/index_" + page + ".html";
			break;
		case 4:
			url = "http://news.pconline.com.cn/digital/index_" + page + ".html";
			break;
		case 5:
			url = "http://news.pconline.com.cn/software/index_" + page + ".html";
			break;
		case 6:
			url = "http://news.pconline.com.cn/science/index_" + page + ".html";
			break;
		default:
			url = "http://news.pconline.com.cn/index_" + page + ".html";
			break;
		}
		System.out.println(newType);
		System.out.println(url);
		Document pcolineDoc = Jsoup.parse(new URL(url), 10000);// Jsoup.parse(pconlineHtml.replace("&nbsp;", " "));
		Elements lis = pcolineDoc.select("div.list-wrap ul li");
		List<SearchListEntity> list = new ArrayList<>();
		for (int i = 0; i < lis.size(); i++) {
			SearchListEntity sle = new SearchListEntity();

			String tittle = lis.get(i).select("dt a").text();
			String newsUrl = lis.get(i).select("li dt a").attr("href");
			String summary = lis.get(i).select("dd span.des").text();
			String viewTimes = "0";
			String time = new String(lis.get(i).select("dd .date").text().getBytes(), "utf-8").replace("|??", "");
			String author = "pconline";
			// 隐藏的li中的img连接为src2
			String imgurl = i < 22 ? lis.get(i).select("i a img").attr("#src")
					: lis.get(i).select("i a img").attr("src2");

			sle.setAuthor(author);
			sle.setImgurl(imgurl);
			sle.setSummary(summary);
			sle.setTime(time);
			sle.setTittle(tittle);
			sle.setUrl(newsUrl);
			sle.setViewTimes(viewTimes);
			// 第5个li和第11个li不是标题li，加个判断过滤一下
			if (!(newsUrl == null || newsUrl == "")) {
				list.add(sle);
			}
		}
		return list;
	}

	@Override
	public ArticleEntity getNewsEntity(String url) throws IOException {

		Document pcolineDoc = Jsoup.parse(new URL(url), 10000);// Jsoup.parse(pconlineHtml.replace("&nbsp;", " "));

		ArticleEntity article = new ArticleEntity();

		article.setContent(
				pcolineDoc.select("div.content").html().replace("<img src", "<img alt").replace("#src", "src"));
		article.setContentText(pcolineDoc.select("div.content").text());
		article.setTime(pcolineDoc.select("span.pubtime").text());
		article.setViewTimes("0");
		article.setTittle(pcolineDoc.select("h1.content-tit-1").text());
		article.setUrl(url);
		article.setOriginal("pconline");
		article.setAuthor(pcolineDoc.select("span.source").text().replace("出处/作者：", ""));
		article.setTags("it新闻");
		return article;
	}

}
